package org.col.dspace.tagger.submit.step;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.col.dspace.tagger.model.TagGenerator;
import org.col.dspace.tagger.model.TagGeneratorFactory;
import org.dspace.app.util.DCInputsReader;
import org.dspace.app.util.DCInputsReaderException;
import org.dspace.app.util.SubmissionInfo;
import org.dspace.app.util.Util;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.DCValue;
import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.submit.AbstractProcessingStep;

/**
 * 
 * @author intern
 * @version May 9 2011
 *
 */
public class TagStep extends AbstractProcessingStep{

	/* Button to generate tags*/
	public static final String SUBMIT_GENERATE_TAG_BUTTON = "submit_generate_tag";

	private static DCInputsReader inputsReader = null;
	private static Logger log=Logger.getLogger(TagStep.class);

	
/*	public TagStep() throws ServletException{
		getInputReader();
		
	}
	
	private DCInputsReader getInputReader() throws ServletException {
		// load inputsReader only the first time
        if (inputsReader == null)
        {
            // read configurable submissions forms data
            try
            {
                inputsReader = new DCInputsReader();
            }
            catch (DCInputsReaderException e)
            {
                throw new ServletException(e);
            }
        }
        
        return inputsReader;
		
	}*/

	@Override
	public int doProcessing(Context context, HttpServletRequest request,
			HttpServletResponse response, SubmissionInfo subInfo)
			throws ServletException, IOException, SQLException,
			AuthorizeException {
		
		// get button user pressed
        String buttonPressed = Util.getSubmitButton(request, NEXT_BUTTON);
        log.info("submit button:"+buttonPressed);
        
        if(buttonPressed.equals(SUBMIT_GENERATE_TAG_BUTTON)){
        	//if this is to generate tags
        	
        	//retrieve bistream
        	Bitstream bitstream = subInfo.getBitstream();
        	//retrive bistream format
        	String format = bitstream.getFormat().getMIMEType();
        	ArrayList<String> tags= new ArrayList<String>();
        	TagGenerator tagGen= TagGeneratorFactory.getTagGenerator(bitstream,format);
        	//if tagGen==null, format not supported.
        	if(tagGen==null)
        		return TagGenerator.STATUS_TAG_GEN_WRONG_FORMAT;
        	
        	int status = tagGen.generateTags(tags);
        	
        	if(status == TagGenerator.STATUS_TAG_GEN_SUCCESS){
        		//update metadata of this item
        		Item item = subInfo.getSubmissionItem().getItem();
        		String value= StringUtils.join(tags.iterator(),", ");
        		item.clearMetadata("dc", "description", "tag", Item.ANY);
        		item.addMetadata("dc","description","tag",Item.ANY,value);
        		item.update();
        		
        		saveTagsToRequest(request, tags);
        		
        		log.info("successfully generated tags:" + value);
        		
        	}
        	
        	context.commit();
        	return status;
       }
		

		return STATUS_COMPLETE;
	}
	

	private void saveTagsToRequest(HttpServletRequest request, ArrayList<String> tags){
		request.setAttribute("tags", tags);
		
	}

	@Override
	public int getNumberOfPages(HttpServletRequest request,
			SubmissionInfo subInfo) throws ServletException {
		return 1;
	}
	

}
